<template>
  <div class="music-list-wrapper">
    <div class="title">{{title}}</div>
    <div class="bg-image" ref="bgImage">
      <div class="drop" ref="drop"></div>
      <div
        class="shuffle-play-btn"
        ref="shufflePlayBtn"
        @click="random"
        >
        <span class="icon-play-mini"></span>
        <span class="desc">随机播放全部</span>
      </div>
      <img :src="bgImage" />
    </div>
    <span
      class="icon-back back-btn"
      @click="handleBackClick"
      ></span>
    <scroll
      :data="songs"
      :listenScroll="true"
      :probeType="3"
      class="scroll-wrapper"
      ref="scroll"
      @scroll="handleScroll"
      >
      <song-list
        :songs="songs"
        :rank="rank"
        ref="songList"
        @select="handleSelect"
        ></song-list>
    </scroll>
    <div class="loading-wrap" v-show="!songs.length">
      <loading></loading>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { prefixStyle } from 'common/js/dom'
import SongList from 'base/song-list/song-list'
import Scroll from 'base/scroll/scroll'
import Loading from 'base/loading/loading'
import { playlistMixin } from 'common/js/mixin'

const transform = prefixStyle('transform')

export default {
  name: 'music-list',
  mixins: [ playlistMixin ],
  props: {
    // 背景图片
    bgImage: {
      type: String,
      default: ''
    },
    // 歌曲列表
    songs: {
      type: Array,
      default () {
        return []
      }
    },
    // 页面顶部标题
    title: {
      type: String,
      default: ''
    },
    rank: {
      type: Boolean,
      default: false
    }
  },
  components: {
    SongList,
    Scroll,
    Loading
  },
  data () {
    return {
      bgHeight: 0,
      scrollY: 0
    }
  },
  mounted () {
    // this.$refs.scroll 获取的是 Vue Component 对象，$el 获取的是 DOM
    this.bgHeight = this.$refs.bgImage.offsetHeight
    this.$refs.scroll.$el.style.top = `${this.bgHeight}px`
  },
  methods: {
    handleScroll (pos) {
      // 获取元素到顶部的距离
      const top = this.$refs.songList.$el.getBoundingClientRect().top

      if (top <= 44) {
        // 小于等于 44 时，让图片覆盖住列表并设置高度为 44px
        this.$refs.bgImage.style.zIndex = '10'
        this.$refs.bgImage.style.height = '44px'
        this.$refs.bgImage.style.paddingBottom = '0'
        this.$refs.shufflePlayBtn.style.display = 'none'
      } else {
        // 大于 44 时，恢复原状
        this.$refs.bgImage.style.zIndex = '0'
        this.$refs.bgImage.style.height = '0'
        this.$refs.bgImage.style.paddingBottom = '70%'
        this.$refs.shufflePlayBtn.style.display = 'block'
      }

      this.scrollY = pos.y
    },
    handleBackClick () {
      this.$router.back()
    },
    handleSelect (song, index) {
      // console.log(song, index)
      this.selectPlay({
        list: this.songs,
        index
      })
    },
    random () {
      this.randomPlay({
        list: this.songs
      })
    },
    handlePlaylist (playlist) {
      const bottom = playlist.length ? '60px' : ''
      this.$refs.scroll.$el.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    ...mapActions([
      'selectPlay',
      'randomPlay'
    ])
  },
  watch: {
    scrollY (newVal) {
      let scale = 1 // 缩放值
      const percent = Math.abs(newVal / this.bgHeight)

      let opacity = 0.3

      if (newVal > 0) {
        // newVal > 0 时，表示是向下拉
        scale = 1 + percent
      } else {
        opacity = opacity + percent < 0.8 ? opacity + percent : 0.8
      }

      this.$refs.bgImage.style[transform] = `scale(${scale})`

      this.$refs.drop.style.background = `rgba(0, 0, 0, ${opacity})`
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~common/stylus/variable.styl'
  .music-list-wrapper {
    // position: absolute
    position: fixed
    top: 0
    right: 0
    bottom: 0
    left: 0
    z-index: 100
    background: $color-background
    .title {
      position: absolute
      top: 0
      width: 100%
      text-align: center
      line-height: 44px
      z-index: 100
    }
    .bg-image {
      position: relative
      overflow: hidden
      width: 100%
      height: 0
      padding-bottom: 70%
      background: #eee
      transform-origin: top
      img {
        width: 100%
      }
      .drop {
        position: absolute
        top: 0
        bottom: 0
        width: 100%
        background: $color-background-d
      }
      .shuffle-play-btn {
        position: absolute
        bottom: 16px
        left: 50%
        transform: translateX(-50%)
        box-sizing: border-box
        width: 120px
        height: 28px
        line-height: 28px
        border: 1px solid $color-theme
        border-radius: 14px
        text-align: center
        color: $color-theme
        .desc {
          margin-left: 4px
          font-size: $font-size-small-s
          vertical-align: 2px
        }
      }
    }
    .back-btn {
      position: absolute
      top: 0
      left: 0
      z-index: 101
      font-size: 20px
      padding: 10px
      color: $color-theme
    }
    .scroll-wrapper {
      position: absolute
      right: 0
      bottom: 0
      left: 0
      // overflow: hidden
    }
    .loading-wrap {
      margin-top: 100px
    }
  }
</style>
